package com.glsc.ngateway.platform.controller.it;

import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.it.ItArpDevice;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.platform.service.it.SwitchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/it/switchCall")
@Api(value = "switchcall管理", tags = {"switchcall管理"})
public class SwitchCallController {
    private static final Logger logger = LoggerFactory.getLogger(SwitchCallController.class);
    @Autowired
    private SwitchService switchService;

    @RequestMapping(method = RequestMethod.POST, value = "truncateSwitchData")
    @ApiOperation(value = "truncateSwitchData", notes = "truncateSwitchData")
    public PlatformResponse<?> truncateSwitchData() {
        switchService.truncateSwitchData();
        List<ItArpDevice> checkTruncate = switchService.findAllList();
        return checkTruncate.isEmpty() ? PlatformResponse.success() : PlatformResponse.failedMsg("清空失败");
    }

    @RequestMapping(method = RequestMethod.POST, value = "syncSwitchData")
    @ApiOperation(value = "syncSwitchData", notes = "syncSwitchData")
    public PlatformResponse<?> syncSwitchData(@RequestBody ItArpDevice itArpDevice) {

        String switchIp = itArpDevice.getSwitchIp();
        String switchInterface = itArpDevice.getSwitchInterface();
        String macAddress = itArpDevice.getMacAddress();

        List<ItArpDevice> aSwitch = switchService.findSwitch(switchIp, switchInterface, macAddress);
        if (aSwitch != null && aSwitch.size() > 0) {
            return PlatformResponse.failedMsg("添加失败，数据已存在。" + switchIp + " " + switchInterface + " " + macAddress);
        }
        switchService.saveSwitch(itArpDevice);
        return PlatformResponse.success();
    }


}
